#!/bin/bash

# 检查jq命令是否可用
check_jq() {
    if ! command -v jq &> /dev/null; then
        echo -e "${RED}[错误]${NC} 未找到 jq 命令。请先安装 jq:"
        echo "    - Ubuntu/Debian: sudo apt-get install jq"
        echo "    - CentOS/RHEL:  sudo yum install jq"
        echo "    - Alpine:       apk add jq"
        exit 1
    fi
}

# 安装路径配置
# 默认安装路径
DEFAULT_INSTALL_PATH="/usr/local/bin"
# 实际安装路径，可通过环境变量覆盖
INSTALL_PATH="${CUSTOM_INSTALL_PATH:-$DEFAULT_INSTALL_PATH}"
SOFTWARE_NAME="fdsmastergo"
INSTALL_FULL_PATH="${INSTALL_PATH}/${SOFTWARE_NAME}"

# 定义颜色
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 打印带颜色的信息
info() {
    echo -e "${GREEN}[信息]${NC} $1"
}

warn() {
    echo -e "${YELLOW}[警告]${NC} $1"
}

error() {
    echo -e "${RED}[错误]${NC} $1"
    exit 1
}

# 检查是否以root权限运行
check_root() {
    if [ "$EUID" -ne 0 ]; then
        error "请使用root权限运行此脚本"
    fi
}

# 检查安装环境
check_install_env() {
    # 检查jq命令
    check_jq
    
    # 检查安装目录是否存在
    if [ ! -d "$INSTALL_FULL_PATH" ]; then
        error "未找到安装目录: $INSTALL_FULL_PATH"
    fi

    # 检查services.json是否存在
    if [ ! -f "$INSTALL_FULL_PATH/services.json" ]; then
        error "未找到配置文件: $INSTALL_FULL_PATH/services.json"
    fi
} 